package com.bite.bite_forum.controller;
import com.bite.bite_forum.common.AppConfig;
import com.bite.bite_forum.common.AppResult;
import com.bite.bite_forum.common.ResultCode;
import com.bite.bite_forum.exception.ApplicationException;
import com.bite.bite_forum.model.Message;
import com.bite.bite_forum.model.User;
import com.bite.bite_forum.services.IMessageService;
import com.bite.bite_forum.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "站内信接口")
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    IMessageService messageService;
    @Resource
    IUserService userService;

    @ApiOperation("发送信")
    @PostMapping ("/send")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("接收者") @RequestParam("receiveUserId")  @NonNull Long receiveUserId,
                            @ApiParam("正文") @RequestParam("content") @NonNull String content){
        //验证发送者
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.info(ResultCode.FAILED_USER_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //验证接收者
        User receiveUser = userService.selectByPrimaryKey(receiveUserId);
        if(receiveUser == null || receiveUser.getDeleteState() == 1){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }

        //封装对象
        Message message = new Message();
        message.setReceiveUserId(receiveUserId);
        message.setPostUserId(user.getId());
        message.setContent(content);
        messageService.create(message);
        return AppResult.success();
    }
    
    @ApiOperation("获取未回复数")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        //获取登录用户
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Integer result = messageService.selectUnreadCount(user.getId());
        return AppResult.success(result);
    }

    @ApiOperation("获取站内信列表")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpSession session){
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        List<Message> result = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(result);
    }

    /**
     * 更新站内信状态
     * @param id
     * @return
     */

    @ApiOperation("更新已读")
    @PostMapping("/markRead")
    public AppResult markRead(HttpSession session,
                              @ApiParam("站内信id") @RequestParam("id") @NonNull Long id){
        //校验站内信是否存在
        Message message = messageService.selectById(id);
        if(message == null || message.getDeleteState() == 1){
            log.info(ResultCode.FAILED_MESSAGE_NOT_EXISTS.getMessage());
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }

        //信是否是自己的
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getId() != message.getReceiveUserId()){
            log.info(ResultCode.FAILED_UNAUTHORIZED.getMessage());
            return  AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        //更新状态
        messageService.updateStateById(id, (byte) 1);
        return AppResult.success();
    }

    /**
     * 回复信
     * @param session
     * @param repliedId
     * @param content
     * @return
     */

    @ApiOperation("回复信")
    @PostMapping("/reply")
    public AppResult reply(HttpSession session,
                           @ApiParam("要回复信的id") @RequestParam("repliedId") @NonNull Long repliedId,
                           @ApiParam("正文") @RequestParam("content") @NonNull String content){
        //验证用户状态
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.info(ResultCode.FAILED_USER_BANNED.getMessage());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //验证回复信是否存在
        Message existsMessage = messageService.selectById(repliedId);
        if(existsMessage == null || existsMessage.getDeleteState() == 1){
            log.info(ResultCode.FAILED_MESSAGE_NOT_EXISTS.getMessage());
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //不能自己给自己回复
        if(user.getId() == existsMessage.getPostUserId()){
            return AppResult.failed("不能给自己回信");
        }
        //封装对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(existsMessage.getPostUserId());
        message.setContent(content);
        messageService.reply(repliedId, message);
        return AppResult.success();
    }
}
